/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AddCartPack;
import DBManager.DBConnection;
import java.sql.*;
import DBManager.DBConnection;

/**
 *
 * @author maconcon
 */
public class CartManager {
 
       public boolean addCart(Cart cart,int x)
    {
        boolean result = false;
        
        try{
            //get connection
            DBConnection db = new DBConnection();
            Connection conn =  db.getConnection();
            
          
            PreparedStatement pstmt =  conn.prepareStatement("insert into cartitems(transid,customerid,productid)"+
                "values(?,?,?)");
int i=1;
            pstmt.setInt(i++,x);
            pstmt.setInt(i++, cart.getCustomerid());
                 pstmt.setInt(i++, cart.getProductid());
            //append values for the parameters
    

            //execute SQL statement
            pstmt.executeUpdate();
            
            result = true;
        }
        catch(SQLException ex){ex.printStackTrace();}
        
        return result;
    }
       
        public boolean updateCart(Cart cart)
    {
        boolean result = false;
        
        try{
            //get connection
            DBConnection db = new DBConnection();
            Connection conn =  db.getConnection();
            
          
            PreparedStatement pstmt =  conn.prepareStatement("UPDATE cartitems SET quantity=? WHERE cartid=?");
int i=1;
            pstmt.setInt(i++, cart.getQuanity());
            pstmt.setInt(i++, cart.getCartid());
                 //pstmt.setInt(i++, cart.getProductid());
            //append values for the parameters
    

            //execute SQL statement
            pstmt.executeUpdate();
            
            result = true;
        }
        catch(SQLException ex){ex.printStackTrace();}
        
        return result;
    }
         public boolean deleteCart(Cart cart)
    {
        boolean result = false;
        
        try{
            //get connection
            DBConnection db = new DBConnection();
            Connection conn =  db.getConnection();
            
          
            PreparedStatement pstmt =  conn.prepareStatement("delete from cartitems where cartid=?");
int i=1;
            //pstmt.setInt(i++, cart.getQuanity());
            pstmt.setInt(i++, cart.getCartid());
                 //pstmt.setInt(i++, cart.getProductid());
            //append values for the parameters
    

            //execute SQL statement
            pstmt.executeUpdate();
            
            result = true;
        }
        catch(SQLException ex){ex.printStackTrace();}
        
        return result;
    }
       
            public int addTransaction(Cart cart)
    {
   
         int result=0;
        try{
            //get connection
            DBConnection db = new DBConnection();
            Connection conn =  db.getConnection();
            ResultSet rs = null;
         
            PreparedStatement pstmt =  conn.prepareStatement("insert into transaction (customerid) value(?)",PreparedStatement.RETURN_GENERATED_KEYS);
int i=1;
            //pstmt.setInt(i++, cart.getQuanity());
            pstmt.setInt(i++, cart.getCustomerid());
            pstmt.executeUpdate();
            
          rs=pstmt.getGeneratedKeys();
            if(rs.next()){
                result=rs.getInt(1);
            }
      
        
        }catch(SQLException ex){ex.printStackTrace();}
     
          return result;
    }
       
}
